package com.solome.net;

import java.io.IOException;
import java.nio.ByteBuffer;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Net.Protocol;
import com.badlogic.gdx.net.Socket;
import com.badlogic.gdx.net.SocketHints;
import com.badlogic.gdx.utils.Array;
import com.solome.base.Logger;

public class Connection implements Runnable, ConnectionImpl, java.io.Closeable {
	private Array<ByteBuffer> array = new Array<ByteBuffer>();
	String _host;
	int _port;
	Socket socket = null;
	Connect connect;

	@Override
	public void setHost(String host) {
		// TODO Auto-generated method stub
		this._host = host;
	}

	@Override
	public String getHost() {
		// TODO Auto-generated method stub
		return this._host;
	}

	@Override
	public void setPort(int port) {
		// TODO Auto-generated method stub
		this._port = port;
	}

	@Override
	public int getPort() {
		// TODO Auto-generated method stub
		return this._port;
	}

	@Override
	public void setConnect(Connect connect) {
		// TODO Auto-generated method stub
		this.connect = connect;
	}

	@Override
	public Connect getConnect() {
		// TODO Auto-generated method stub
		return this.connect;
	}

	@Override
	public void connectOperate(String host, int port) {
		// TODO Auto-generated method stub
		if (this.getConnect() == null)
			throw new IllegalArgumentException("Connect null.");
		switch (this.getConnect()) {
		case SOCKET:
			SocketHints hints = new SocketHints();
			hints.connectTimeout = 5000;
			hints.socketTimeout = 5000;
			socket = Gdx.net.newClientSocket(Protocol.TCP, host, port, hints);
			break;
		case HTTP:
			break;
		default:
			throw new IllegalArgumentException(
					"Connect IllegalArgumentException.");
		}
		this.setHost(host);
		this.setPort(port);
	}

	@Override
	public byte[] warp() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void close() throws IOException {
		// TODO Auto-generated method stub
		if (socket != null) {
			socket.dispose();
		}
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		while (true) {
			Logger.error(this, "执行" + socket.isConnected());
		}
	}

	// @Override
	// public Thread newThread(Runnable r) {
	// // TODO Auto-generated method stub
	// return null;
	// }
}
